using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Biz;

using EntLibEC.Info.Basic;
using EntLibEC.Biz.Basic;

using EntLibEC.Info.Purchase;
using EntLibEC.Biz.Purchase;

namespace EntLibEC.View.Purchase
{
	/// <summary>
	/// Summary description for POBasketOpt.
	/// </summary>
	public partial class POBasketOpt : PageBase
	{
	

		protected EntLibEC.View.Cmn.StockProduct ucProduct;
		private int sysNo;
        private int productSysNo;
		private string opt;
		protected string bodyOnLoad;

		protected void Page_Load(object sender, System.EventArgs e)
		{
			
			if ( Request.QueryString["sysno"] != null)
			{
				try
				{
                    this.sysNo = Convert.ToInt32(Request.QueryString["sysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "sysno is not an integer", -1);
					return;
				}
			}
			else
			{
				Assert(lblMessage, "sysno missing", -1);
				return;
			}
            if (Request.QueryString["productsysno"] != null)
            {
                try
                {
                    this.productSysNo = Convert.ToInt32(Request.QueryString["productsysno"].ToString());
                }
                catch
                {
                    Assert(lblMessage, "product sysno is not an integer", -1);
                    return;
                }
            }
            else
            {
                Assert(lblMessage, "sysno missing", -1);
                return;
            }
			if ( Request.QueryString["opt"] == null )
			{
				Assert(lblMessage, "opt missing", -1);
				return;
			}
			else
			{
				this.opt = Request.QueryString["opt"].ToString();
			}

			InitForm();
			if ( !Page.IsPostBack)
			{
				BindForm();
			}
		}
		private void BindForm()
		{	
			if ( opt.ToLower() == "update")
			{
				POBasketInfo oInfo = PurchaseManager.GetInstance().LoadBasket(this.sysNo);
				txtPOBasketQty.Text = oInfo.Quantity.ToString();
				txtOrderPrice.Text = oInfo.OrderPrice.ToString(AppConst.DecimalFormat);
			}
		}
		private void InitForm()
		{
			ucProduct.ProductSysNo = this.productSysNo;
			ucProduct.Enabled = false;

			btnAdd.Visible = false;
			btnUpdate.Visible = false;

			if ( opt.ToLower() == "add")
			{
				txtPOBasketQty.Text = "1";
				btnAdd.Visible = true;
				AddPOBasket();
			}
			else
			{
				btnUpdate.Visible = true;
			}
			
		}
		private void AddPOBasket()
		{
			if (!CheckForm())
				return;
			try
			{
				POBasketInfo oInfo = new POBasketInfo();
				oInfo.CreateUserSysNo = base.sInfo.User.SysNo;
				oInfo.ProductSysNo = this.sysNo;
				oInfo.Quantity = Convert.ToInt32(txtPOBasketQty.Text.Trim());
				oInfo.OrderPrice = Convert.ToDecimal(txtOrderPrice.Text.Trim());
				
				PurchaseManager.GetInstance().InsertPOBasket(oInfo);

				LogManager.GetInstance().Write(new LogInfo(oInfo.SysNo, (int)AppEnum.LogType.Purchase_Basket_Insert, base.sInfo));
				bodyOnLoad = "javascript:window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert po basket error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}

		}
		private bool CheckForm()
		{
			//productsysno
			//qty > 0
			if ( ucProduct.ProductSysNo == AppConst.IntNull)
			{
				Assert(lblMessage, "product id or sysno can't be empty", -1);
				return false;
			}
			if ( txtPOBasketQty.Text.Trim() == "" )
			{
				Assert(lblMessage, "Qty can't be empty", -1);
				return false;
			}
			try
			{
				int qty = Convert.ToInt32(txtPOBasketQty.Text.Trim());
				if ( qty ==0 )
				{
					Assert(lblMessage, "quantity can't be zero",-1);
					return false;
				}
			}
			catch
			{
				Assert(lblMessage, "quantity should be an integer", -1);
				return false;
			}
			try
			{
				decimal orderPrice = Convert.ToDecimal(txtOrderPrice.Text.Trim());
				if  ( orderPrice < 0 )
				{
					Assert(lblMessage, "order price should be greater than zero", -1);
				}
			}
			catch
			{
				Assert(lblMessage, "order price should be decimal", -1);
				return false;
			}
			return true;
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		protected void btnAdd_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.POFillIn, false, lblMessage) )
				return;
			AddPOBasket();
		}

		protected void btnUpdate_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.POFillIn, false, lblMessage) )
				return;
			if (!CheckForm())
				return;
			try
			{

				POBasketInfo oInfo = PurchaseManager.GetInstance().LoadBasket(this.productSysNo, base.sInfo.User.SysNo);
				oInfo.Quantity = Convert.ToInt32(txtPOBasketQty.Text.Trim());
				oInfo.OrderPrice = Convert.ToDecimal(txtOrderPrice.Text.Trim());
				
				PurchaseManager.GetInstance().UpdatePOBasket(oInfo);

				LogManager.GetInstance().Write(new LogInfo(oInfo.SysNo, (int)AppEnum.LogType.Purchase_Basket_Update, base.sInfo));
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnRefresh','btnRefresh');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert adjust item error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		
		}
	}
}
